package itsm.isperp.module.service.project;

import itsm.isperp.framework.core.context.ContextHolder;
import itsm.isperp.framework.data.domain.DataRequest;
import itsm.isperp.framework.data.domain.DataResponse;
import itsm.isperp.framework.data.domain.PageResponse;
import itsm.isperp.framework.domain.IUser;
import itsm.isperp.framework.service.BaseService;
import itsm.isperp.framework.utils.HibernateUUIDGenerator;
import itsm.isperp.framework.web.request.JqGridFilterRuleOp;
import itsm.isperp.framework.web.request.JqGridRequest;
import itsm.isperp.module.entity.app.AppUser;
import itsm.isperp.module.entity.project.ProjProject;
import itsm.isperp.module.entity.project.ProjProjectMember;
import itsm.isperp.module.repository.project.ProjProjectMemberMapper;
import itsm.isperp.module.service.app.AppUserService;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

/**
 * 项目人员管理服务类
 * 
 * @author lizx
 * @date 2013-3-2
 * @version V1.0
 * 
 */
@Service
public class ProjProjectMemberService extends BaseService<ProjProjectMember> {

	@Autowired
	protected ProjProjectMemberMapper projProjectMemberMapper;

	@Override
	public ProjProjectMemberMapper getRepository() {
		return this.projProjectMemberMapper;
	}

	/**
	 * 获取项目组人员列表
	 * 
	 * @param projectId
	 *            项目Id
	 * @return 项目人员列表
	 */
	public List<ProjProjectMember> findAllByProjectId(String projectId) {
		return this.projProjectMemberMapper.findAllByProjectId(projectId);
	}

	public List<? extends IUser> findMemberUserByProjectIdAndRole(
			String projectId, String roleId) {
		return this.projProjectMemberMapper.findMemberUserByProjectIdAndRole(
				projectId, roleId);
	}
	
	public static String  findMemberUserNameByProjectId(
      String projectId) {
    String sql="SELECT GROUP_CONCAT(app_user_name) rs FROM proj_project_member WHERE proj_project_id='"+projectId+"' GROUP BY proj_project_id";
    JdbcTemplate jdbcTemplate=ContextHolder.getSpringBean("jdbcTemplate");
   List<Map<String,Object>> list= jdbcTemplate.queryForList(sql);
   if(list.size()>0)
   {
     return list.get(0).get("rs").toString();
   }else
   {
     return null;
   }
	}

	/**
	 * 获取项目组人员列表
	 * 
	 * @param projectId
	 *            项目Id
	 * @return 项目人员列表
	 */
	public List<ProjProject> findMemberProjects(String userName) {
		JqGridRequest req = new JqGridRequest();
		req.isPageable(false);
		req.setSelectColumns("id", "name");
		req.addFilterRule("app_user_name", JqGridFilterRuleOp.eq, userName);
		req.addFilterRule("stage", JqGridFilterRuleOp.eq,
				ProjProjectService.STAGE_PUBLICED);
		return this.projProjectMemberMapper.findMemberProjects(req);
	}

	/**
	 * 
	 * 查询项目人员列表
	 * 
	 * @param req
	 *            数据请求
	 * @return 数据结果
	 */
	public PageResponse<Map<String, Object>> findAllDetailByRequest(
			JqGridRequest request) {

		return this.projProjectMemberMapper.findAllDetailByRequest(request)
				.toPage();
	}

	/**
	 * 
	 * 查询项目可选的人列表
	 * 
	 * @param req
	 *            数据请求
	 * @return 数据结果
	 */
	public DataResponse<Map<String, Object>> findAllMemberByRequest(
			String projectId, DataRequest req) {

		return this.projProjectMemberMapper.findAllDetailOptionalByRequest(
				projectId, req);
	}

	public boolean userExistsInProject(String username, String projectId) {

		JqGridRequest req = new JqGridRequest();
		req.isOnlyQueryCount();
		req.addFilterRule("app_user_name", JqGridFilterRuleOp.eq, username);
		req.addFilterRule("proj_project_id", JqGridFilterRuleOp.eq, projectId);

		long l = this.findAllCount(req.getFilter());
		return l > 0;
	}
	
	public void saveProjectMember(String projectId,String userName)
	{
	  List<ProjProjectMember> list = this.projProjectMemberMapper.findAllByRequest(
        new JqGridRequest()
            .isPageable(false)
            .addFilterRule("app_user_name", JqGridFilterRuleOp.eq,
                userName)
            .addFilterRule("proj_project_id", JqGridFilterRuleOp.eq,
                projectId)).getContent();

    if (list != null && list.size() > 0) {
     
    }else
    {
      AppUserService appUserService=ContextHolder.getSpringBean("appUserService");
      AppUser user=appUserService.findByUsername(userName, "fullname","name","position");
      ProjProjectMember ur = new ProjProjectMember();
      ur.setId(HibernateUUIDGenerator.generate().toString());
      ur.setAppUserName(userName);
      ur.setProjProjectId(projectId);
      ur.setMemberName(user.getFullname());
      this.projProjectMemberMapper.save(ur);
    }
	}

}
